<?php

/**
 * Description of Admin_Controller
 *
 * @author elmerenges
 */
class Controller_Admin extends Controller_Site {

    public function action_index() {
        if (Session::instance()->get('admin') == '1') {
            $this->request->redirect('admin/tasks');
        } else {
            if ($this->request->method() == 'POST') {
                try {
                    $admin = ORM::factory('admin')->where('name', '=', $this->request->post('name'))->find();
                    if ($admin->name != NULL && (sha1($this->request->post('password')) == $admin->password)) {
                        $this->setFlash('notice', 'Sikeres belépés!');

                        Session::instance()->set('admin', '1');
                        $this->request->redirect('admin/tasks');
                    } else {
                        $this->setFlash('notice', 'Sikertelen belépés!');
                        $this->request->redirect('admin/index');
                    }
                } catch (ORM_Validation_Exception $e) {
                    $this->content->set('errors', $e->errors('models'));
                }
            }
        }
    }

    public function action_tasks() {
        if (Session::instance()->get('admin') == '1') {
            $news = ORM::factory('news')->find_all();
            $meetings = ORM::factory('meetings')->find_all();
            $categories = ORM::factory('category')->find_all();
            $categories2 = $categories;
            
            $active_meetings = ORM::factory('meetings')->where('sub_date', '>', DB::expr('NOW()'))->find_all();
            
            $nm = ORM::factory('meetings')->where('active', '=', '1');
            if($nm->id==NULL){
                $no_meeting = true;
            }
            else{
                $no_meeting = false;
            }
            $this->content->set('no_meeting', $no_meeting);
            
            $ckeditor = new CKEditor();
            $config = array(
                array('Source', '-',
                    'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-',
                    'Undo', 'Redo', '-',
                    'Find', 'Replace', '-',
                    'SelectAll', '-',
                    'Maximize', 'ShowBlocks'),
                array('Bold', 'Italic', 'Underline', 'Strike', '-',
                    'Subscript', 'Superscript', '-',
                    'NumberedList', 'BulletedList', '-',
                    'Outdent', 'Indent', 'Blockquote', '-',
                    'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', '-',
                    'Link', 'Unlink', 'Anchor', '-',
                    'Image', 'Flash', 'Table', 'HorizontalRule', 'SpecialChar'
                ),
                array('Format', 'Font', 'FontSize', '-', 'TextColor', 'BGColor')
            );

            $download_form = View::factory('downloads/upload')
                    ->set('meetings', $meetings)
                    ->set('categories', $categories);
            $this->content->set('download_form', $download_form);

            $category_form = View::factory('downloads/categories');
            $this->content->set('category_form', $category_form);
            
            $file_list = View::factory('downloads/file_list')
                    ->set('categories', $categories);
            $this->content->set('file_list', $file_list);

            $links = ORM::factory('page')->where('name', '=', 'links')->find();
            $contact = ORM::factory('page')->where('name', '=', 'contact')->find();
            $home = ORM::factory('page')->where('name', '=', 'home')->find();
            $participate = ORM::factory('page')->where('name', '=', 'participate_form')->find();

            $page_form = View::factory('page/form')
                    ->set('links', $links)
                    ->set('participate', $participate)
                    ->set('contact', $contact)
                    ->set('home', $home)
                    ->set('ckeditor', $ckeditor)
                    ->set('config', $config);
            
            $this->content->set('page_form', $page_form)
                    ->set('news', $news)
                    ->set('meetings', $meetings)
                    ->set('links', $links)
                    ->set('ckeditor', $ckeditor)
                    ->set('config', $config)
                    ->set('categories', $categories)
                    ->set('active_meetings', $active_meetings);
        } else {
            $this->request->redirect('news');
        }
    }

    public function action_logout() {
        Session::instance()->delete('admin');
        $this->request->redirect($this->request->referrer());
    }
}
?>
